﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

// EmguCV
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;


namespace RVBike
{
    class WebcamController : Singleton<WebcamController>
    {
        private Capture             m_kCapture = null;
        private int                 m_nCaptureDeviceIdx = -1;

        public WebcamController()
        {

        }

        ~WebcamController()
        {
        }

        public void Init()
        {
            bool bFoundCam = false;
            foreach (string name in WebcamHelper.EnumerateCaptureDevice())
            {
                bFoundCam = true;
            }

            if (bFoundCam)
                CaptureDevice = 0;
        }

        public void Unint()
        {
            if (m_kCapture != null)
            {
                m_kCapture.Dispose();
                m_kCapture = null;
            }

            m_nCaptureDeviceIdx = -1;
        }

        public int CaptureDevice
        {
            get 
            {
                return m_nCaptureDeviceIdx;
;
            }
            set
            {
                if (m_nCaptureDeviceIdx != value)
                {
                    m_nCaptureDeviceIdx = value;
                    m_kCapture = new Capture(m_nCaptureDeviceIdx);
                    m_kCapture.FlipHorizontal = true;
                }
            }
        }

        public Image<Bgr, Byte> GetCaptureImage()
        {
            if (m_kCapture != null)
            {
                return m_kCapture.QueryFrame();
            }
            else
                return null;
        }


        public int GetCaptureWidth()
        {
            return m_kCapture.Width;
        }

        public int GetCaptureHeight()
        {
            return m_kCapture.Height;
        }
        
    }
}